// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Breakable Wall Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 51 : TYPE_BREAKABLEWALL

private alias object.propertyValue	: player.character // taken from current script aliases

// Function declarations
reserve function BreakableWall_DebugDraw
reserve function BreakableWall_DebugSpawn

// Static Values
public value BreakableWall_value34 = 0;

// Tables

function BreakableWall_DebugDraw
	temp0 = object[0].value17
	temp0 -= BreakableWall_value34
	DrawSprite(temp0)
end function


function BreakableWall_DebugSpawn
	temp0 = object[0].value17
	temp0 -= BreakableWall_value34
	CreateTempObject(TypeName[Breakable Wall], temp0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 4
end function


event ObjectMain
	if object.propertyValue > 2
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x7000
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
	else
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			checkResult = 0
			if object[currentPlayer].state == PlayerObject_HandleRoll
				if object[currentPlayer].speed > 0x47FFF
					checkResult = 1
				end if
				if object[currentPlayer].speed < -0x47FFF
					checkResult = 1
				end if
			end if
			if player[currentPlayer].character == 2 // this *was* stage.playerListPos, but that allowed for Tails to break walls as a sidekick with Knuckles.
				checkResult = 1						// this player.character variable directly checks what player the Player Objects are.
			end if									// basically, if you are Knuckles as either character, the wall will break upon contact for only Knuckles.
			if PlayerObject_SuperState == 1
				checkResult = 1
			end if
			if object[currentPlayer].state == PlayerObject_HandleAir
				if object[currentPlayer].value37 == 3
					if object[currentPlayer].animation == ANI_JUMPING
						if object[currentPlayer].speed > 0x47FFF
							checkResult = 1
						end if
						if object[currentPlayer].speed < -0x47FFF
							checkResult = 1
						end if
					end if
				end if
			end if
			if object[currentPlayer].animation == ANI_HAMMER_SPEEN
				checkResult = 1
			end if
			if object[currentPlayer].state == PlayerObject_HandleYuriBladeAir
				checkResult = 1
			end if
			if object[currentPlayer].state == PlayerObject_HandleYuriBladeGround
				checkResult = 1
			end if
			if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
				if object[0].value1 > 20
					checkResult = 1
				end if	
			end if
			if checkResult == 1
				BoxCollisionTest(C_TOUCH, object.entityPos, -17, -32, 17, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					object.type = TypeName[Blank Object]
					PlaySfx(SfxName[Block Break], 0)
					temp0 = object.propertyValue
					temp0 <<= 1
					temp0 += 3
					temp1 = temp0
					temp1++
					temp2 = object.xpos
					temp2 -= 0x80000
					temp3 = object.xpos
					temp3 += 0x80000
					temp4 = object.ypos
					temp4 -= 0x180000
					if object[currentPlayer].xpos < object.xpos
						CreateTempObject(TypeName[Breakable Wall], temp0, temp2, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = -0x60000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], temp1, temp3, temp4)
						object[tempObjectPos].xvel = -0x40000
						object[tempObjectPos].yvel = -0x50000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], temp0, temp2, temp4)
						object[tempObjectPos].xvel = -0x80000
						object[tempObjectPos].yvel = -0x20000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], temp1, temp3, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = -0x10000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], temp0, temp2, temp4)
						object[tempObjectPos].xvel = -0x80000
						object[tempObjectPos].yvel = 0x20000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], temp1, temp3, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = 0x10000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], temp0, temp2, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = 0x60000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], temp1, temp3, temp4)
						object[tempObjectPos].xvel = -0x40000
						object[tempObjectPos].yvel = 0x50000
						object[tempObjectPos].drawOrder = 4
					else
						CreateTempObject(TypeName[Breakable Wall], temp0, temp2, temp4)
						object[tempObjectPos].xvel = 0x40000
						object[tempObjectPos].yvel = -0x50000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], temp1, temp3, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = -0x60000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], temp0, temp2, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = -0x10000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], temp1, temp3, temp4)
						object[tempObjectPos].xvel = 0x80000
						object[tempObjectPos].yvel = -0x20000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], temp0, temp2, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = 0x10000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], temp1, temp3, temp4)
						object[tempObjectPos].xvel = 0x80000
						object[tempObjectPos].yvel = 0x20000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000
						CreateTempObject(TypeName[Breakable Wall], temp0, temp2, temp4)
						object[tempObjectPos].xvel = 0x40000
						object[tempObjectPos].yvel = 0x50000
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Breakable Wall], temp1, temp3, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = 0x60000
						object[tempObjectPos].drawOrder = 4
					end if
					if object[currentPlayer].animation == ANI_GLIDING
						object[currentPlayer].animation = ANI_GLIDING_DROP
						object[currentPlayer].state = PlayerObject_KnuxGlideDrop
						if object[currentPlayer].direction == FLIP_X
							FlipSign(object[currentPlayer].speed)
						end if
					end if
				end if
			else
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
				temp7 = checkResult
				CheckLower(object[0].value1, 20)
				temp7 &= checkResult
				if temp7 == 0	
					BoxCollisionTest(C_BOX, object.entityPos, -16, -32, 16, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				end if	
			end if
		next
	end if
end event


event ObjectDraw
	DrawSprite(object.propertyValue)
end event


event ObjectStartup
	LoadSpriteSheet("GHZ/Objects.gif")
	foreach (TypeName[Breakable Wall], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
	next
	SpriteFrame(-16, -32, 32, 64, 191, 146)
	SpriteFrame(-16, -32, 32, 64, 207, 146)
	SpriteFrame(-16, -32, 32, 64, 223, 146)
	SpriteFrame(-8, -8, 16, 16, 191, 146)
	SpriteFrame(-8, -8, 16, 16, 207, 146)
	SpriteFrame(-8, -8, 16, 16, 207, 146)
	SpriteFrame(-8, -8, 16, 16, 223, 146)
	SpriteFrame(-8, -8, 16, 16, 223, 146)
	SpriteFrame(-8, -8, 16, 16, 239, 146)
	temp0 = 0
	BreakableWall_value34 = DebugMode_ObjCount
	while temp0 < 3
		SetTableValue(TypeName[Breakable Wall], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(BreakableWall_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(BreakableWall_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
